package cn.fay.chapter14;

public class Individual implements Comparable<Individual> {

	private static long counter=0;
	private static final long id=counter++;
	private String name;
	public Individual(String name){
		this.name=name;
	}
	public Individual(){}
	
	@Override
	public String toString() {
		return getClass().getSimpleName()+(name==null?"":" "+name);
	}
	public long id(){return id;}
	
	@Override
	public boolean equals(Object obj) {
		return obj instanceof Individual&&id==((Individual)obj).id;
	}
	
	@Override
	public int hashCode() {
		int result=17;
		if (name!=null) 
			result=result*37+name.hashCode();
		result=result*37+(int)id;
		return result;
	}
	@Override
	public int compareTo(Individual arg) {
		String first=getClass().getSimpleName();
		String argFirst=arg.getClass().getSimpleName();
		int firstCompare=first.compareTo(arg.name);
		if (firstCompare!=0) 
			return firstCompare;
		if (name!=null&&arg.name!=null) {
			int secondCompare=name.compareTo(arg.name);
			if (secondCompare!=0) 
				return secondCompare;
		}
		return (arg.id<id?-1:(arg.id==id?0:1));
	}
}
